package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.ExperimentWaybill;
import com.siwei.mes.service.experiment.ExperimentWaybillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账与运单关系(ExperimentWaybill)表控制层
 *
 * @author makejava
 * @since 2024-05-27 21:37:50
 */
@Api(value = "试验台账与运单关系")
@RestController
@RequestMapping("experimentWaybill")
public class ExperimentWaybillController {
    @Resource
    private ExperimentWaybillService experimentWaybillService;

    @ApiOperation(value = "新增试验台账与运单关系")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) ExperimentWaybill entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentWaybillService.create(entity)));
    }

    @ApiOperation(value = "修改试验台账与运单关系")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentWaybill entity) {
        experimentWaybillService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除试验台账与运单关系")
    @PostMapping("/delete")
    public Json delete(@RequestBody ExperimentWaybill entity) {
        experimentWaybillService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取试验台账与运单关系")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ExperimentWaybill, ExperimentWaybill> query) {
        return Json.succ().data(experimentWaybillService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取试验台账与运单关系")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentWaybillService.loadById(id));
    }

    @ApiOperation(value = "获取所有试验台账与运单关系")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentWaybill entity) {
        return Json.succ().data(ApiResult.addList(experimentWaybillService.getAll(entity)));
    }

    @ApiOperation(value = "删除试验委托和运单关系")
    @PostMapping(value = "deleteByExperimentIdAndWaybillId")
    public Json deleteByExperimentIdAndWaybillId(@RequestBody ExperimentWaybill entity) {
        Long experimentId = entity.getExperimentId();//试验委托id
        Long waybillId = entity.getWaybillId();//小票id
        assertTrue(experimentId == null, "试验委托id为空");
        assertTrue(waybillId == null, "运单id为空");
        experimentWaybillService.deleteByExperimentIdAndWaybillId(experimentId, waybillId);
        return Json.succ();
    }

}
